#version 330

struct Material{
    float shininess;
    vec3 specularColor;
};
struct DirectionalLight{
    vec3 osDirection;
    vec3 ambientColor;
    vec3 diffuseColor;
} ;

uniform sampler2D diffuseMap;
uniform sampler2D normalMap;
uniform Material material;
uniform DirectionalLight light;

//Per fragment input
in vec3 f_tsVertexToLight;
in vec3 f_tsHalfAngle;
in vec2 f_uv;

out vec4 fragColor;
void main (){
    //float diffuseIntensity=f_normal*
    vec4 normalFrag= texture(normalMap, f_uv);
    vec3 tsNormal = 2. * normalFrag.xyz - 1.;
    #ifdef no_normal_map
    tsNormal =vec3(0,0,1);
    #endif

    vec3 norm_tsVertexToLight = normalize(f_tsVertexToLight);
    float diffuseFactor = max(0., dot(tsNormal, norm_tsVertexToLight) );

    float specularFactor=0;
    if(diffuseFactor>0.){
        vec3 norm_tsHalfAngle = normalize(f_tsHalfAngle);
        specularFactor= max(0., dot(tsNormal, norm_tsHalfAngle) );
        float specularFactor2=specularFactor*specularFactor;
        specularFactor=specularFactor2*specularFactor2;
        //float specularFactor8=specularFactor4*specularFactor4;
    }

    vec3 materialDiffuseColor = texture(diffuseMap, f_uv).rgb;
    #ifdef no_diffuse_map
    materialDiffuseColor=vec3(1,1,1);
    #endif
    float dustFactor=min(normalFrag.a*2. + 0.7 ,1);


    fragColor.rgb =
    + materialDiffuseColor * ( diffuseFactor*light.diffuseColor + light.ambientColor ) * dustFactor
    + specularFactor * material.specularColor * light.diffuseColor
    ;
    
    fragColor.a=1;
}
